package com.edu.etu.gui.checkit;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;

public class EditTemplateActivity extends ActionBarActivity implements View.OnClickListener{
    Cursor cursor;
    LinearLayout llMain;
    ImageButton btnAddElement;
    ImageButton btnSave;

    EditText element;

    TextView NameTemplate;

    SQLiteHelperList openHelperList;
    SQLiteDatabase db;
    ContentValues cv;

    Integer idTemplate;
    EditText input;
    private HashMap<View,Integer> mapViewTemplate = new HashMap<View,Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_template);
        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();
        btnAddElement=(ImageButton) findViewById(R.id.btnAddElemTemp);
        btnAddElement.setOnClickListener(this);

        btnSave=(ImageButton) findViewById(R.id.btnSaveTemp);
        btnSave.setOnClickListener(this);

        NameTemplate = (TextView) findViewById(R.id.NameTemplate);
        element = (EditText) findViewById(R.id.ElementTemp);
        llMain = (LinearLayout) findViewById(R.id.llMain);

        openHelperList = new SQLiteHelperList(getApplicationContext());
        db = openHelperList.getWritableDatabase();
        cv = new ContentValues();

        cursor = db.query("ListsTemplates", null, null, null, null, null, null);
        if(cursor.moveToFirst())
        {
            do {
                if(cursor.getInt(cursor.getColumnIndex("_id4"))==getIntent().getIntExtra("TemplateId",-1))
                {
                    NameTemplate.setText(cursor.getString(cursor.getColumnIndex("nameListsTemplates")));
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();

        cursor = db.query("Template", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            int ListsTemplates = cursor.getColumnIndex("ListsTemplates");
            int nameElementsTemplate = cursor.getColumnIndex("nameElementsTemplate");
            int boolTemplate = cursor.getColumnIndex("boolTemplate");
            int idElement = cursor.getColumnIndex("_id3");
            do {
                if(getIntent().getIntExtra("TemplateId",-1)==cursor.getInt(ListsTemplates))
                {
                    LayoutInflater inflater = getLayoutInflater();
                    View list_element = inflater.inflate(R.layout.template_element,null,false);
                    TextView element_text_view =  (TextView)list_element.findViewById(R.id.template_element_text);
                    element_text_view.setText(cursor.getString(nameElementsTemplate));
                    element_text_view.setOnClickListener(this);
                    ImageButton imgButtonDelete = (ImageButton) list_element.findViewById(R.id.btnDeleteTemplate);
                    imgButtonDelete.setOnClickListener(this);
                    ImageButton imgButtonFix = (ImageButton) list_element.findViewById(R.id.btnFixTemplate);
                    imgButtonFix.setOnClickListener(this);
                    llMain.addView(list_element);
                    mapViewTemplate.put(element_text_view,cursor.getInt(idElement));
                    mapViewTemplate.put(imgButtonDelete, cursor.getInt(idElement));
                    mapViewTemplate.put(imgButtonFix, cursor.getInt(idElement));
                }
            }
            while (cursor.moveToNext());
        }
        cursor.close();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.edit_template, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        Cursor l = db.query("ListsTemplates", null, null, null, null, null, null);
        l.moveToLast();
        idTemplate=mapViewTemplate.get(v);

        if(idTemplate!=null)
        {
            switch (v.getId())
            {
                case R.id.btnDeleteTemplate:
                    ImageButton ib = (ImageButton)v;
                    LinearLayout ll = (LinearLayout)ib.getParent();
                    ll.setVisibility(View.GONE);
                    cursor = db.query("Template", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(idTemplate==cursor.getInt(cursor.getColumnIndex("_id3")))
                            {
                                String i = cursor.getString(cursor.getColumnIndex("_id3"));
                                if (i.equalsIgnoreCase("")) {
                                    break;
                                }
                                db.execSQL("delete from Template where _id3='"+i+"'");

                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                    break;
                case R.id.btnFixTemplate:
                    Log.d("myLog","fix");

                    AlertDialog.Builder adb = new AlertDialog.Builder(this);
                    input = new EditText(this);
                    cursor = db.query("Template", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(idTemplate==cursor.getInt(cursor.getColumnIndex("_id3")))
                            {
                                input.setText(cursor.getString(cursor.getColumnIndex("nameElementsTemplate")));
                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                    adb.setTitle(getString(R.string.EditingElement));
                    adb.setView(input);
                    adb.setNegativeButton(R.string.no, myClickListener);
                    adb.setPositiveButton(R.string.yes, myClickListener);
                    adb.show();
                    break;
            }
        }
        switch (v.getId())
        {
            case R.id.btnAddElemTemp:
                Log.d("myLog","add");
                LayoutInflater inflater = getLayoutInflater();
                View list_element = inflater.inflate(R.layout.template_element,null,false);
                TextView element_text_view =  (TextView)list_element.findViewById(R.id.template_element_text);
                if(element.length()==0)element.setText("Element");
                element_text_view.setText(element.getText().toString());
                ImageButton imgButtonDelete = (ImageButton) list_element.findViewById(R.id.btnDeleteTemplate);
                ImageButton imgButtonFix = (ImageButton) list_element.findViewById(R.id.btnFixTemplate);
                llMain.addView(list_element);
                imgButtonDelete.setOnClickListener(this);
                imgButtonFix.setOnClickListener(this);
                cv.put(openHelperList.idListsTemplates, l.getString(l.getColumnIndex("_id4")));
                cv.put(openHelperList.nameElementsTemplate, element.getText().toString());
                cv.put(openHelperList.boolTemplate, 0);
                db.insert(openHelperList.NameTableTemplate,null,cv);
                element.setText(null);
                cursor = db.query("Template", null, null, null, null, null, null);
                if (cursor.moveToLast()) {
                    int _id2 = cursor.getColumnIndex("_id3");
                    mapViewTemplate.put(element_text_view, cursor.getInt(_id2));
                    mapViewTemplate.put(imgButtonDelete, cursor.getInt(_id2));
                    mapViewTemplate.put(imgButtonFix, cursor.getInt(_id2));
                }
                cursor.close();
                break;
            case R.id.btnSaveTemp:
                startActivity(new Intent(this, Template.class));
                finish();
                break;



        }

    }
    DialogInterface.OnClickListener myClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case Dialog.BUTTON_POSITIVE:
                    cursor = db.query("Template", null, null, null, null, null, null);
                    if (cursor.moveToFirst()) {
                        do {
                            if(idTemplate==cursor.getInt(cursor.getColumnIndex("_id3")))
                            {
                                String strFilter = "_id3="+idTemplate;
                                ContentValues args = new ContentValues();
                                args.put(openHelperList.nameElementsTemplate,input.getText().toString());
                                db.update(openHelperList.NameTableTemplate,args,strFilter,null);
                                finish();
                                startActivity(getIntent());
                            }
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                    break;
                case Dialog.BUTTON_NEGATIVE:

                    break;
            }
        }
    };
}

